/**
* 基础菜单 菜品管理 菜品列表
*/
<template>
  <div>
    <!-- 面包屑导航 -->
    <el-breadcrumb separator-class="el-icon-arrow-right">
      <el-breadcrumb-item :to="{ path: '/' }">首页</el-breadcrumb-item>
      <el-breadcrumb-item :to="{path:'#'}">菜品管理</el-breadcrumb-item>
      <el-breadcrumb-item>菜品列表</el-breadcrumb-item>
    </el-breadcrumb>
    <!-- 搜索筛选 -->
    <el-form :inline="true" :model="formInline" class="user-search">
      <el-form-item style="margin-left: 56px;">
        <el-button size="small" @click="clickDishesStatus(1)">在售菜品</el-button>
        <el-button size="small" @click="clickDishesStatus(2)">下架菜品</el-button>
      </el-form-item>
      <br />
      <el-form-item style="margin-right: 40px;margin-left: 30px;">
        <el-button size="small" type="primary" icon="el-icon-plus" @click="handleEdit()" style="margin-right: 24px;">发布菜品</el-button>
        <el-button size="small" type="success" icon="el-icon-refresh" @click="clickRefresh">刷新</el-button>
      </el-form-item>
      <el-form-item label="搜索：">
        <el-input size="small" v-model="dishesInfo.dishes_name" placeholder="菜品名称"></el-input>
      </el-form-item>
      <el-form-item>
        <el-button size="small" type="primary" icon="el-icon-search" @click="search">搜索</el-button>
      </el-form-item>
    </el-form>
    <!--列表-->
    <el-table size="small" :data="listData" highlight-current-row v-loading="loading" border element-loading-text="拼命加载中"
      style="width: 100%;">
      <el-table-column align="center" type="index" label="序号" width="60">
      </el-table-column>
      <el-table-column align="center" prop="dishes_name" label="菜品名称" width="120">
      </el-table-column>
      <el-table-column align="center" sortable prop="dishes_price" label="价格" width="80">
      </el-table-column>
      <el-table-column align="center" prop="category_name" label="所属分类" width="100">
      </el-table-column>
      <el-table-column align="center" prop="dishes_monthly_sales" label="销量" width="80">
      </el-table-column>
      <el-table-column align="center" prop="dishes_repertory" label="库存" width="80">
      </el-table-column>
      <el-table-column align="center" prop="dishes_hot_sell_name" label="是否热销" width="100">
      </el-table-column>
      <el-table-column align="center" prop="dishes_new_product_name" label="是否新品" width="100">
      </el-table-column>
      <el-table-column align="center" prop="dishes_recommend_name" label="是否推荐" width="100">
      </el-table-column>
      <el-table-column align="center" sortable prop="add_time" label="增加时间" width="140">
        <template slot-scope="scope">
          <div>{{scope.row.add_time|timestampToTime}}</div>
        </template>
      </el-table-column>
      <el-table-column align="center" sortable prop="update_time" label="修改时间" width="140">
        <template slot-scope="scope">
          <div>{{scope.row.update_time|timestampToTime}}</div>
        </template>
      </el-table-column>
      <el-table-column align="center" prop="username" label="操作员" width="120">
      </el-table-column>
      <el-table-column align="center" label="操作" min-width="100">
        <template slot-scope="scope">
          <el-button size="mini" type="warning" @click="handleEdit(scope.$index, scope.row)">编辑</el-button>
          <el-button size="mini" type="danger" @click="deleteDishes(scope.$index, scope.row)">删除</el-button>
        </template>
      </el-table-column>
    </el-table>
    <!-- 分页组件 -->
    <Pagination v-bind:child-msg="pageparm" @callFather="callFather"></Pagination>
    <!-- 编辑界面 -->
    <el-dialog :title="title" :visible.sync="editFormVisible" width="30%" @click="closeDialog">
      <el-form label-width="120px" :model="editForm" :rules="rules" ref="editForm">
        <el-form-item label="菜品名称" prop="dishes_name">
          <el-input size="small" v-model="editForm.dishes_name" auto-complete="off" placeholder="请输入菜品名称"></el-input>
        </el-form-item>
        <el-form-item label="所属分类" prop="category_id">
          <el-select v-model="editForm.category_id" placeholder="请选择商品分类">
            <template v-for="item in category">
              <el-option :value="item.category_id" :label="item.category_name" :key="item.category_id"></el-option>
            </template>
          </el-select>
        </el-form-item>
        <!-- <el-form-item label="菜品封面" prop="dishes_cover">
          <el-upload>
            <el-button size="small" type="primary">点击上传</el-button>
            <div slot="tip" class="el-upload__tip">只能上传jpg/png文件，且不超过500kb</div>
          </el-upload>
        </el-form-item> -->
        <el-form-item label="菜品描述" prop="dishes_desc">
          <el-input type="textarea" rows="6" size="small" v-model="editForm.dishes_desc" auto-complete="off"
            placeholder="请输入菜品描述"></el-input>
        </el-form-item>
        <el-form-item label="菜品价格" prop="dishes_price">
          <el-input-number size="small" v-model="editForm.dishes_price" auto-complete="off"></el-input-number><span
            style="margin-left: 12px;">元</span>
        </el-form-item>
        <el-form-item label="菜品库存" prop="dishes_repertory">
          <el-input-number size="small" v-model="editForm.dishes_repertory" auto-complete="off"></el-input-number><span
            style="margin-left: 12px;">件</span>
        </el-form-item>
        <el-form-item v-if="title == '编辑'" label="是否下架" prop="dishes_status">
          <el-radio v-model="editForm.dishes_status" :label="2" border size="medium" :checked="editForm.dishes_status?'':'checked'">是</el-radio>
          <el-radio v-model="editForm.dishes_status" :label="1" border size="medium" :checked="editForm.dishes_status?'checked':''">否</el-radio>
        </el-form-item>
        <el-form-item label="是否热销" prop="dishes_hot_sell">
          <el-radio v-model="editForm.dishes_hot_sell" :label="1" border size="medium" :checked="editForm.dishes_hot_sell?'':'checked'">是</el-radio>
          <el-radio v-model="editForm.dishes_hot_sell" :label="2" border size="medium" :checked="editForm.dishes_hot_sell?'checked':''">否</el-radio>
        </el-form-item>
        <el-form-item label="是否新品" prop="dishes_new_product">
          <el-radio v-model="editForm.dishes_new_product" :label="1" border size="medium" :checked="editForm.dishes_new_product?'':'checked'">是</el-radio>
          <el-radio v-model="editForm.dishes_new_product" :label="2" border size="medium" :checked="editForm.dishes_new_product?'checked':''">否</el-radio>
        </el-form-item>
        <el-form-item label="是否推荐" prop="dishes_recommend">
          <el-radio v-model="editForm.dishes_recommend" :label="1" border size="medium" :checked="editForm.dishes_recommend?'':'checked'">是</el-radio>
          <el-radio v-model="editForm.dishes_recommend" :label="2" border size="medium" :checked="editForm.dishes_recommend?'checked':''">否</el-radio>
        </el-form-item>
      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button size="small" @click="closeDialog">取消</el-button>
        <el-button size="small" type="primary" :loading="loading" class="title" @click="submitForm(title)">保存</el-button>
      </div>
    </el-dialog>
  </div>
</template>

<script>
  import {
    dishestList,
    categoryList,
    dishestAdd,
    dishestUpdate,
    dishestDelete,
  } from '../../api/adminMG'
  import {
    getStore
  } from '../../utils/util'
  import Pagination from '../../components/Pagination'
  export default {
    data() {
      return {
        category: [],
        nshow: true, //switch开启
        fshow: false, //switch关闭
        loading: false, //是显示加载
        editFormVisible: false, //控制编辑页面显示与隐藏
        title: '添加',
        editForm: {
          dishes_name: '',
          dishes_price: 0,
          category_id: '',
          dishes_desc: '',
          // dishes_cover:'',
          dishes_status: '',
          dishes_hot_sell: '',
          dishes_new_product: '',
          dishes_recommend: '',
          dishes_repertory:0,
          // token: localStorage.getItem('logintoken')
        },
        // rules表单验证
        rules: {
          dishes_name: [{
            required: true,
            message: '请输入菜品名称',
            trigger: 'blur'
          }],
          dishes_price: [{
            required: true,
            message: '请输入菜品价格',
            trigger: 'blur'
          }],
          category_id: [{
            required: true,
            message: '请选择所属分类',
            trigger: 'blur'
          }],
          dishes_desc: [{
            required: true,
            message: '请输入菜品描述',
            trigger: 'blur'
          }],
          dishes_repertory: [{
            required: true,
            message: '请输入菜品库存',
            trigger: 'blur'
          }]
        },
        formInline: {
          page: 1,
          limit: 10,
          varLable: '',
          varName: '',
          token: localStorage.getItem('logintoken')
        },
        dishesInfo:{
          dishes_status:0,
          dishes_name:''
        },
        // 删除部门
        seletedata: {
          ids: '',
          token: localStorage.getItem('logintoken')
        },
        userparm: [], //搜索权限
        listData: [], //用户数据
        // 分页参数
        pageparm: {
          currentPage: 1,
          pageSize: 10,
          total: 10
        }
      }
    },
    // 注册组件
    components: {
      Pagination
    },
    /**
     * 数据发生改变
     */

    /**
     * 创建完毕
     */
    created() {
      this.getDishestList(this.dishesInfo);
      this.getCategory();
    },

    /**
     * 里面的方法只有被调用才会执行
     */
    methods: {
      // 获取菜品列表
      getDishestList(parameter) {
        this.loading = true
        dishestList(parameter)
          .then(res => {
            this.loading = false
            if (res.status == 0) {
              this.$message({
                type: 'success',
                message: "获取数据成功"
              })
              console.log("请求菜品列表", res.data.dishesList)
              this.listData = res.data.dishesList
              //   // 分页赋值
              //   this.pageparm.currentPage = this.formInline.page
              //   this.pageparm.pageSize = this.formInline.limit
              //   this.pageparm.total = res.count
            } else {
              this.$message({
                type: 'warning',
                message: "获取菜品列表失败"
              })
            }
          })
          .catch(err => {
            this.loading = false
            this.$message.error('菜品加载失败，请稍后再试！')
          })
      },
      //刷新页面，再次请求菜品列表接口
      clickRefresh() {
        this.getDishestList(this.dishesInfo);
      },
      //在售or下线菜品
      clickDishesStatus(dishesStatus){
        if(dishesStatus == 1){  //在售菜品
          this.dishesInfo.dishes_status = 1;
        }else if(dishesStatus == 2){    //下线菜品
          this.dishesInfo.dishes_status = 2;
        }
        this.getDishestList(this.dishesInfo)
      },
      //获取分类列表
      getCategory(parameter) {
        var that = this;
        categoryList(parameter)
          .then(res => {
            this.loading = false
            if (res.status == 0) {
              console.log("请求分类列表", res.data.categoryList)
              that.category = res.data.categoryList;
              if (this.category.length > 0) {
                this.show_input = true
              } else {
                this.$message({
                  showClose: true,
                  message: "请先创建商品分类",
                  type: "warning"
                });
              }
            } else {
              this.$message({
                type: 'warning',
                message: "获取分类列表失败"
              })
            }
          })
          .catch(err => {
            this.loading = false
            this.$message.error('分类加载失败，请稍后再试！')
          })
      },
      // 分页插件事件
      callFather(parm) {
        this.formInline.page = parm.currentPage
        this.formInline.limit = parm.pageSize
        this.getDishestList(this.dishesInfo)
      },
      // 搜索事件
      search() {
        this.getDishestList(this.dishesInfo)
        this.getCategory();
      },
      //显示编辑界面
      handleEdit: function(index, row) {
        this.editFormVisible = true
        if (row != undefined && row != 'undefined') {
          this.title = '编辑'
          // this.editForm = row
          this.editForm.dishes_id = row.dishes_id
          this.editForm.dishes_name = row.dishes_name
          this.editForm.dishes_price = row.dishes_price
          this.editForm.category_id = row.category_id
          this.editForm.dishes_desc = row.dishes_desc
          this.editForm.dishes_cover = row.dishes_cover
          this.editForm.dishes_status = row.dishes_status
          this.editForm.dishes_hot_sell = row.dishes_hot_sell
          this.editForm.dishes_new_product = row.dishes_new_product
          this.editForm.dishes_recommend = row.dishes_recommend
          this.editForm.dishes_repertory = row.dishes_repertory
        } else {
          this.title = '添加'
          this.editForm = {}
          // this.editForm.dishes_id = ''
          // this.editForm.dishes_name = ''
          this.editForm.dishes_price = ''
          this.editForm.dishes_repertory = ''
          // this.editForm.category_name = ''
          // this.editForm.dishes_desc = ''
        }
      },
      // 编辑、增加页面保存方法
      submitForm(editData) {
        console.log("editData", editData)
        this.editForm.staff_id = getStore('staffInfo').staff_id;
        console.log("获取编辑菜品表单", this.editForm)
        if(editData == '添加'){
          this.editForm.dishes_cover = "https://gimg2.baidu.com/image_search/src=http%3A%2F%2Fww2.sinaimg.cn%2Fthumb150%2F593793e5gw1esosy3pokjj20c80953zg.jpg&refer=http%3A%2F%2Fwww.sina.com&app=2002&size=f9999,10000&q=a80&n=0&g=0n&fmt=jpeg?sec=1630820523&t=0a12743856cd6d02fad8b35b5c35ea60";
          this.addDishes();
        }else if(editData == '编辑'){
          this.updateDishes();
        }
      },
      //菜品添加
      addDishes(){
        dishestAdd(this.editForm)
          .then(res => {
            this.editFormVisible = false
            this.loading = false
            console.log("菜品添加", res)
            if (res.data.result) {
              this.$message({
                type: 'success',
                message: "菜品添加成功"
              })
              this.getDishestList(this.dishesInfo);
            } else {
              this.$message({
                type: 'warning',
                message: "菜品添加失败"
              })
            }
          })
          .catch(err => {
            this.editFormVisible = false
            this.loading = false
            this.$message.error('菜品添加失败，请稍后再试！')
          })
      },
      //菜品编辑
      updateDishes(){
        dishestUpdate(this.editForm)
          .then(res => {
            this.editFormVisible = false
            this.loading = false
            console.log("菜品编辑", res)
            if (res.data.result) {
              this.$message({
                type: 'success',
                message: "菜品编辑成功"
              })
              this.getDishestList(this.dishesInfo);
              // console.log("请求菜品列表", res.data.dishesList)
              // this.listData = res.data.dishesList
            } else {
              this.$message({
                type: 'warning',
                message: "菜品编辑失败"
              })
            }
          })
          .catch(err => {
            this.editFormVisible = false
            this.loading = false
            this.$message.error('菜品编辑失败，请稍后再试！')
          })
      },
      // 删除菜品
      deleteDishes(index, row) {
        this.$confirm('确定要删除吗?', '信息', {
            confirmButtonText: '确定',
            cancelButtonText: '取消',
            type: 'warning'
          })
          .then(() => {
            dishestDelete(row.dishes_id)
              .then(res => {
                console.log("菜品删除", res)
                if (res.data.result) {
                  this.$message({
                    type: 'success',
                    message: "菜品删除成功"
                  })
                  this.getDishestList(this.dishesInfo);
                  // console.log("请求菜品列表", res.data.dishesList)
                  // this.listData = res.data.dishesList
                } else {
                  this.$message({
                    type: 'warning',
                    message: "菜品删除失败"
                  })
                }
              })
              .catch(err => {
                this.loading = false
                this.$message.error('菜品删除失败，请稍后再试！')
              })
          })
          .catch(() => {
            this.$message({
              type: 'info',
              message: '已取消删除'
            })
          })
      },
      // 关闭编辑、增加弹出框
      closeDialog() {
        this.editFormVisible = false
      }
    }
  }
</script>

<style scoped>
  .user-search {
    margin-top: 20px;
  }

  .userRole {
    width: 100%;
  }
</style>
